package com.chaoyong.higo.activity;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.PrefUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Login2Activity extends Activity implements OnClickListener {

	private Activity context;
	private EditText et_phone, et_password;
	private Button btn;
	private ImageView img;
	private TextView nickname;
	private String uid;
	
	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login2);
		context = this;
		initViews();
	}

	private void initViews() {
		// TODO Auto-generated method stub
		
		base_left_iv = (LinearLayout) findViewById(R.id.base_left_iv);
		base_title_tv = (TextView) findViewById(R.id.base_title_tv);
		base_right_tv = (TextView) findViewById(R.id.base_right_tv);

		base_title_tv.setText("完善信息");
		base_right_tv.setText("注册");
		
		base_left_iv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(context, LoginActivity.class));
				context.finish();
			}
		});
		
		et_phone = (EditText) findViewById(R.id.login2_phone);
		et_password = (EditText) findViewById(R.id.login2_pwd);
		btn = (Button) findViewById(R.id.login2_btn);
		btn.setOnClickListener(this);
		uid = getIntent().getStringExtra("uid");
		/*img = (ImageView) findViewById(R.id.login2_img);
		nickname = (TextView) findViewById(R.id.login2_nickname);
		nickname.setText(getIntent().getStringExtra("nickname"));
		ImageLoader.getInstance().displayImage(getIntent().getStringExtra("headimgurl"), img);*/
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		final String phone = et_phone.getText().toString().trim();
		String password = et_password.getText().toString().trim();
		switch (v.getId()) {
		case R.id.login2_btn:
			if(TextUtils.isEmpty(phone)){
				Toast.makeText(context, "请输入手机号", 0).show();
				return ;
			}
			if(TextUtils.isEmpty(password)){
				Toast.makeText(context, "请输入支付密码", 0).show();
				return ;
			}
			JSONObject json = new JSONObject();
			try {
				json.put("phone", phone);
				json.put("pay", password);
				json.put("uid", uid);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			RequestParams params = new RequestParams();
			try {
				params.setBodyEntity(new StringEntity(json.toString(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			HttpUtils utils = new HttpUtils();
			utils.send(HttpMethod.POST, OApi.Public_wxinfo, params, new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					// TODO Auto-generated method stub
					//Toast.makeText(context, arg0.result, 1).show();\
					String result = arg0.result;
					try {
						JSONObject js = new JSONObject(result);
						if(js.getInt("status") == 0){
							Toast.makeText(context, js.getString("data"), 0).show();
						}
						if(js.getInt("status") == 1){
							OApi.uid = uid;
							PrefUtils.setBoolean(context, "isLogin", true);
							PrefUtils.setString(context, "userId", uid);
							PrefUtils.setString(context, "userPhone", phone);
							startActivity(new Intent(context, MainActivity.class));
							context.finish();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
			break;

		default:
			break;
		}
	}
}
